<?php

namespace App\Traits\Relation;

use App\Models\Company;
use App\Models\Permission;
use App\Models\System\Module;
use Illuminate\Database\Eloquent\Relations\MorphOne;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;

/**
 * 用于定义系统模块和其他模型的关系
 *
 * Trait ModuleRelation
 * @package App\Traits\Relation
 * Date: 22/03/2018
 * @author George
 */
trait ModuleRelation
{
	/**
	 * 获取购买模块的公司信息
	 *
	 * Date: 22/03/2018
	 * @author George
	 * @return BelongsToMany
	 */
	public function companies()
	{
		return $this->belongsToMany(Company::class, 'company_module_pivot', 'module_code', 'company_id', 'code', 'id');
	}

	/**
	 * 获取子模块信息
	 *
	 * Date: 22/03/2018
	 * @author George
	 * @return mixed
	 */
	public function children()
	{
		return $this->hasMany(Module::class, 'parent_code', 'code');
	}

	/**
	 * 获取模块对应的权限
	 *
	 * Date: 23/03/2018
	 * @author George
	 * @return MorphOne
	 */
	public function permission()
	{
		return $this->morphOne(Permission::class, 'permissiontable', 'module', 'code', 'code');
	}
}
